//
//  GJMapViewManager.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/8/24.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "MapViewManager.h"
#import "MapView.h"
#import <React/RCTUIManager.h>
#import <React/RCTDefines.h>
@interface MapViewManager ()
@property (nonatomic, strong)MapView *mapView;
@property (nonatomic, strong)NSDictionary *iconNameDic;
@end

@implementation MapViewManager
//标记宏
RCT_EXPORT_MODULE()

- (UIView *)view{
  return self.mapView;
}

#pragma mark -- method

RCT_EXPORT_METHOD(onCreate:(nonnull NSNumber *)reactTag){
  
}
RCT_EXPORT_METHOD(onDestroy:(nonnull NSNumber *)reactTag){
  dispatch_async(dispatch_get_main_queue(), ^{
    self.mapView = nil;
  });
}
RCT_EXPORT_METHOD(startLocation:(nonnull NSNumber *)reactTag){
  //定位
  dispatch_async(dispatch_get_main_queue(), ^{
    [self getLoaction];
  });
}

RCT_EXPORT_METHOD(drawOverlay:(nonnull NSNumber *)reactTag icon:(NSDictionary*)icon list:(NSDictionary *)list){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, MapView *> *viewRegistry) {
    MapView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[MapView class]]) {
//      RCTLogError(@"Invalid view returned from registry, expecting mapView, got: %@", view);
    } else {
      //画标记点
      dispatch_async(dispatch_get_main_queue(), ^{
        NSString *iconName = self.iconNameDic[icon[@"icon"]];
        [self.mapView addAnnotationsWithIcon:[iconName gj_hasValue] ? iconName : @"mapModule_position_selected" list:list[@"list"]?list[@"list"]:@[]];
      });
    }
  }];
  
}

RCT_EXPORT_METHOD(drawLine:(nonnull NSNumber *)reactTag list:(NSDictionary *)list){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, MapView *> *viewRegistry) {
    MapView *view = viewRegistry[reactTag];
    if (![view isKindOfClass:[MapView class]]) {
//      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
    } else {
      //画线
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.mapView addLineToMapView:list];
      });
    }
  }];
}
RCT_EXPORT_METHOD(drawOverlayTotal:(nonnull NSNumber *)reactTag list:(NSDictionary *)list){
  [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, MapView *> *viewRegistry) {
      MapView *view = viewRegistry[reactTag];
      if (![view isKindOfClass:[MapView class]]) {
  //      RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
      } else {
        //画线
        dispatch_async(dispatch_get_main_queue(), ^{
          [self.mapView addCarNumberAnnotation:list[@"list"]?list[@"list"]:@[]];
        });
      }
  }];
}
RCT_EXPORT_METHOD(setCenter:(nonnull NSNumber *)reactTag list:(NSDictionary *)list){
  if (list[@"lat"] && list[@"lon"]) {
    dispatch_async(dispatch_get_main_queue(), ^{
      [self.mapView setMapCenterWithLocation:list];
    });
  }
}
#pragma mark -- property


RCT_EXPORT_VIEW_PROPERTY(resIndex, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(locationIcon, NSString)

#pragma mark -- actions
/// 定位
- (void)getLoaction{
  [[GJLocationUtil shareInstance] getLocationWithCommplete:^(CLLocationCoordinate2D coordinate, NSDictionary * _Nonnull addressDic, NSError * _Nonnull error) {
    [[GJLocationUtil shareInstance] stopSerialLocation];
    if (error == nil) {
      
      NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:addressDic];
      [dic setObject:@(coordinate.latitude?coordinate.latitude:0) forKey:@"latitude"];
      [dic setObject:@(coordinate.longitude?coordinate.longitude:0) forKey:@"longitude"];
      [GJUtil sendNotificationForRNEventEmitterWithActionKey:@"LOCATION_RESULT" parameter:dic];
      [self.mapView showUserLocationAnnotation:dic];
    }else{
      if (![GJUtil hasLocationAuthorization]) {
        [UIAlertController showAlertInViewController:[UIApplication sharedApplication].keyWindow.rootViewController withTitle:@"请允许访问位置信息" message:@"我们将获取您的位置信息，用于展示你的当前地址" cancelButtonTitle:@"取消" destructiveButtonTitle:@"同意" otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
              if (buttonIndex == 1) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
              }
        }];
        return;
      }
    }
  }];
}


#pragma mark -- getter
- (MapView *)mapView{
  if (!_mapView) {
    _mapView = [MapView new];
  }
  return _mapView;
}
- (NSDictionary *)iconNameDic{
  if (!_iconNameDic) {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"RNIconNameToOC"ofType:@"plist"];

    NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];


    _iconNameDic = dictionary;
  }
  return _iconNameDic;
}
@end
